package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Address;
import com.qf.zyh.entity.Cart;
import com.qf.zyh.entity.Orders;
import com.qf.zyh.entity.User;
import com.qf.zyh.services.AddressSer;
import com.qf.zyh.services.CartSer;
import com.qf.zyh.services.Impl.AddressSerImpl;
import com.qf.zyh.services.Impl.CartSerImpl;
import com.qf.zyh.services.Impl.OrderSerImpl;
import com.qf.zyh.services.OrderSer;
import com.qf.zyh.util.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/order")
public class OrderCont extends BaseServlet{
    CartSer cs = new CartSerImpl();
    AddressSer as = new AddressSerImpl();
    OrderSer os = new OrderSerImpl();
    //订单预览展示
    public String getorder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //订单页面会展示用户的收货地址和购物车数据，要获取购物车和的收货地址，通过用户的id
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        if (user==null) {
            return Const.SysConst.REDIRECT+"login.jsp";
        }
        List<Cart> all = cs.getAll(user.getUid());
        List<Address> addlist = as.seleAddr(user.getUid());

        request.setAttribute("cartList",all);
        request.setAttribute("addList",addlist);

        return Const.SysConst.FORWARD+"order.jsp";
    }
    //添加订单
    public String addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        if (user==null) {
            return Const.SysConst.REDIRECT+"login.jsp";
        }
        //地址id
        Integer id = StrUtils.toint(request.getParameter("aid"));
        //生成订单，在订单表中。添加后在订单页面展示出来，则返回订单数据
        Orders insor = os.insor(id, user.getUid());
        if (insor!=null) {
            request.setAttribute("yorn","订单添加成功");
            request.setAttribute("order",insor);
            return Const.SysConst.FORWARD+"orderSuccess.jsp";
        }else {
            request.setAttribute("yorn","订单添加失败");
            return Const.SysConst.FORWARD+"orderSuccess.jsp";
        }

    }
    //展示订单，注意：订单预览是从购物车进入，而展示订单是从个人订单中进入
    public String seleorder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute(Const.UserCont.IS_LOGIN);
        if (user==null) {
            return Const.SysConst.REDIRECT+"login.jsp";
        }
        List<Orders> list = os.seleor(user.getUid());
        request.setAttribute("orderList",list);
        return Const.SysConst.FORWARD+"orderList.jsp";
    }
    //订单详情，展示单个订单的数据
    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oid = request.getParameter("oid");
          Orders o =os.getordDetail(oid);
          request.setAttribute("od",o);
          return Const.SysConst.FORWARD+"orderDetail.jsp";
    }
    //确认收货
    public String changeStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oid = request.getParameter("oid");
        boolean b = os.updastate(oid,Const.orderCpde.SP);
        return Const.SysConst.REDIRECT+"order?method=seleorder";
    }

}
